package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 * 检查组管理*/
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    //新增检查组
    @Reference
    private CheckGroupService checkGroupService;

    /*this.checkitemIds, this.formData
        添加2个表
    * */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkGroup, checkitemIds);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
        return new Result();
    }

    //分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    //根据中间表查找CheckGroupId
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {

        try {
            List<Integer> checkGroupIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }

    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroupId, Integer[] checkitemIds) {
        try {
            checkGroupService.edit(checkGroupId, checkitemIds);
            return new Result();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        if (checkGroupList != null && checkGroupList.size() > 0) {
            Result result = new Result(checkGroupList);
            result.setData(checkGroupList);
            return result;
        }
        return new Result(false);
    }
    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return checkGroupService.delete(id);
    }
}
